$(function () {
    let obj = {
        "code": 1,
        "msg": null,
        "data": {
            "records": [
                {
                    "id": "1529436486208946178",
                    "number": "1529436486208946178",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529436452444798978",
                    "orderTime": "2022-05-25 20:17:29",
                    "checkoutTime": "2022-05-25 20:17:29",
                    "payMethod": 1,
                    "amount": 78,
                    "remark": "",
                    "userName": "haha",
                    "phone": "13456789987",
                    "address": "aaaa",
                    "consignee": "张sir"
                },
                {
                    "id": "1529787092127809537",
                    "number": "1529787092127809537",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529436452444798978",
                    "orderTime": "2022-05-26 19:30:39",
                    "checkoutTime": "2022-05-26 19:30:39",
                    "payMethod": 1,
                    "amount": 118,
                    "remark": "",
                    "userName": "haha",
                    "phone": "13456789987",
                    "address": "aaaa",
                    "consignee": "张sir"
                },
                {
                    "id": "1529819313232510977",
                    "number": "1529819313232510977",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529436452444798978",
                    "orderTime": "2022-05-26 21:38:42",
                    "checkoutTime": "2022-05-26 21:38:42",
                    "payMethod": 1,
                    "amount": 78,
                    "remark": "",
                    "userName": "haha",
                    "phone": "13456789987",
                    "address": "aaaa",
                    "consignee": "张sir"
                },
                {
                    "id": "1529820716923453441",
                    "number": "1529820716923453441",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529820425142501377",
                    "orderTime": "2022-05-26 21:44:16",
                    "checkoutTime": "2022-05-26 21:44:16",
                    "payMethod": 1,
                    "amount": 388,
                    "remark": "",
                    "userName": "haha",
                    "phone": "18868816185",
                    "address": "黑马程序员4楼",
                    "consignee": "李四"
                },
                {
                    "id": "1530846066742755330",
                    "number": "1530846066742755330",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529820425142501377",
                    "orderTime": "2022-05-29 17:38:39",
                    "checkoutTime": "2022-05-29 17:38:39",
                    "payMethod": 1,
                    "amount": 302,
                    "remark": "",
                    "userName": "haha",
                    "phone": "18868816185",
                    "address": "黑马程序员4楼",
                    "consignee": "李四"
                },
                {
                    "id": "1531957231623438337",
                    "number": "1531957231623438337",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529820425142501377",
                    "orderTime": "2022-06-01 19:14:01",
                    "checkoutTime": "2022-06-01 19:14:01",
                    "payMethod": 1,
                    "amount": 314,
                    "remark": "",
                    "userName": "haha",
                    "phone": "18868816185",
                    "address": "黑马程序员4楼",
                    "consignee": "李四"
                },
                {
                    "id": "1531957497907216386",
                    "number": "1531957497907216386",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529820425142501377",
                    "orderTime": "2022-06-01 19:15:05",
                    "checkoutTime": "2022-06-01 19:15:05",
                    "payMethod": 1,
                    "amount": 206,
                    "remark": "",
                    "userName": "haha",
                    "phone": "18868816185",
                    "address": "黑马程序员4楼",
                    "consignee": "李四"
                },
                {
                    "id": "1531959386006716417",
                    "number": "1531959386006716417",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529820425142501377",
                    "orderTime": "2022-06-01 19:22:35",
                    "checkoutTime": "2022-06-01 19:22:35",
                    "payMethod": 1,
                    "amount": 211,
                    "remark": "",
                    "userName": "haha",
                    "phone": "18868816185",
                    "address": "黑马程序员4楼",
                    "consignee": "李四"
                },
                {
                    "id": "1531960019539558402",
                    "number": "1531960019539558402",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529820425142501377",
                    "orderTime": "2022-06-01 19:25:06",
                    "checkoutTime": "2022-06-01 19:25:06",
                    "payMethod": 1,
                    "amount": 246,
                    "remark": "",
                    "userName": "haha",
                    "phone": "18868816185",
                    "address": "黑马程序员4楼",
                    "consignee": "李四"
                },
                {
                    "id": "1531960668691988481",
                    "number": "1531960668691988481",
                    "status": 4,
                    "userId": "1",
                    "addressBookId": "1529820425142501377",
                    "orderTime": "2022-06-01 19:27:41",
                    "checkoutTime": "2022-06-01 19:27:41",
                    "payMethod": 1,
                    "amount": 146,
                    "remark": "",
                    "userName": "haha",
                    "phone": "18868816185",
                    "address": "黑马程序员4楼",
                    "consignee": "李四"
                }
            ],
            "total": 63,
            "size": 10,
            "current": 1,
            "orders": [

            ],
            "optimizeCountSql": true,
            "hitCount": false,
            "countId": null,
            "maxLimit": null,
            "searchCount": true,
            "pages": 7
        },
        "map": {

        }
    }

    const q = {
        page: 1,
        pageSize: 10,

    }
    // 渲染页面
    render()
    function render() {
        $.ajax({
            method: 'get',
            // url: 'http://121.4.102.44:8083/order/page?page=' + q.pagenum + '&pageSize=' + q.pagesize,
            url: 'http://121.4.102.44:8083/order/page',
            data: q,
            success(res) {
                console.log(res);
                const htmlStr = template('tpl', res.data)
                $('tbody').html(htmlStr)
                renderPage(res.data.total)
            }
        })

    }


    // 分页功能
    function renderPage(total) {
        layui.laypage.render({
            elem: 'pageBox',
            count: total,
            theme: 'orange',
            limit: q.pageSize,
            curr: q.page,
            layout: ['page', 'next', 'prev', 'count', 'skip', 'limit'],
            limits: [2, 4, 6, 8, 10, 12, 14, 16],
            jump: function (obj, first) {
                // obj 当前最新分页配置项
                // first 渲染分页按钮调用的jump值则为true
                // 如果单击按钮调用jump值则为undefined
                if (!first) {
                    q.page = obj.curr
                    q.pageSize = obj.limit
                    render()
                }
            }
        })
    }




    // 根据订单号和日期筛选数据
    $('.layui-btn-warm').on('click', function (e) {
        e.preventDefault()
        // alert('ok')
        let id = $('[name=order]').val().trim()
        let startTime = $('.startTime').val().trim() + ' 00:00:00'
        let endTime = $('.endTime').val().trim() + ' 23:59:59'
        console.log(id);
        console.log(startTime);
        console.log(endTime);
        if (id === '' && $('.startTime').val().trim() === '' && $('.endTime').val().trim() === '') {
            return render()
        }
        else if (id !== '') {
            $.ajax({
                method: 'get',
                url: 'http://121.4.102.44:8083/order/page?page=1&pageSize=10&number=' + id,
                success(res) {
                    // console.log(res);
                    if (res.data.records.length === 0) {
                        return layui.layer.msg('订单号不存在', function () {
                            $('[name=order]').val('')
                            render()
                        })


                    }
                    const htmlStr = template('tpl', res.data)
                    $('tbody').html(htmlStr)
                    renderPage(res.data.total)
                    $('[name=order]').val('')


                }
            })
        }
        else {
            // $.ajax({
            //     method: 'get',
            //     url: 'http://121.4.102.44:8083/order/page?page=1&pageSize=10&beginTime=' + startTime + '&endTime=' + endTime,
            //     success(res) {
            //         console.log(res);
            //         const htmlStr = template('tpl', res.data)
            //         $('tbody').html(htmlStr)
            //         renderPage(res.data.total)
            //     }
            // })
            const htmlStr = template('tpl', obj.data)
            $('tbody').html(htmlStr)
            renderPage(obj.data.total)
            $('.startTime').val('')
            $('.endTime').val('')


        }





    })



    // <!-- 日历插件 -->
    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#test1' //指定元素
        });
    });


    // // 模态框
    $('tbody').on('click', '.look', function () {
        // console.log('123')
        let value = $(this).attr('data-value')
        console.log(value);
        value = (JSON.parse(value));
        onLook(value)


    })
    // function getStatus(count) {
    //     if (count === 4) {
    //         $('#status').html('已完成')
    //     } else if (count === 3) {
    //         $('#status').html('已派送')
    //     }
    //     else if (count === 2) {
    //         $('#status').html('正在派送')
    //     }
    // }

    // 封装函数
    function onLook(value) {
        $('#dialog-edit').empty();
        $('#dialog-edit').append(
            `<div class="ordermsg">
                <div class="info-box">
                    <div class="item-box"><span class="label">订单号：</span><span class="des">${value.id}</span></div>
                    <div class="item-box"><span class="label">订单状态：</span><span id="status" class="des">${value.status === 4 ? '已完成' : '未完成'}</span></div>
                    <div class="item-box"><span class="label">收货人：</span><span class="des">${value.userName}</span></div>
                    <div class="item-box"><span class="label">联系电话：</span><span class="des">${value.phone}</span></div>
                    <div class="item-box"><span class="label">地址：</span><span class="des">${value.address}</span></div>
                    <div class="item-box"><span class="label">支付金额：</span><span class="des">${value.amount}</span></div>
                    <div class="item-box"><span class="label">下单时间：</span><span class="des">${value.orderTime}</span></div>
                </div>
            </div>`)
        layui.layer.open({
            type: 1,
            title: '订单信息',
            area: ['550px', '350px'],
            content: $('#dialog-edit').html()
        })


    }

    // 改变订单状态
    $('tbody').on('click', '.status', function () {
        const id = $(this).attr('data-id')
        console.log(id);


        // console.log('123')
        layui.layer.open({
            type: 0,
            title: '提示',
            area: ['450px', '190px'],
            content: '确认更改该订单状态？'
            , btn: ['确定', '取消']
            , function(index, layero) {
                //按钮【按钮二】的回调
                //return false 开启该代码可禁止点击该按钮关闭
            }
            , yes: function (index, layero) {
                //按钮【按钮二】的回调

                $('#layui-layer100001').hide()
                $('#layui-layer-shade100001').hide()
                if ($('.status').html() === '派送') {
                    $.ajax({
                        method: 'put',
                        url: 'http://121.4.102.44:8083/order',
                        data: JSON.stringify({
                            id,
                            status: 3
                        }),
                        contentType: "application/json",
                        success(res) {
                            layui.layer.msg('订单已派送', { icon: 1, time: 1000 });
                            console.log(res);
                            render()
                            return
                        }
                    })
                }
                else {
                    $.ajax({
                        method: 'put',
                        url: 'http://121.4.102.44:8083/order',
                        data: JSON.stringify({
                            id,
                            status: 4
                        }),
                        contentType: "application/json",
                        success(res) {
                            layui.layer.msg('订单已完成', { icon: 1, time: 1000 });
                            console.log(res);
                            render()
                        }
                    })
                }
            }
            , cancel: function () {
                //右上角关闭回调
            }
        });
    })

















})